cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/outliers/synth/100pct/
use types_panel.dta, clear  
keep if group == 999

merge 1:1 dt using  synth_fit.dta

local treatment_period = mofd(mdy(11, 4, 2002))
disp `treatment_period'
	* 514

** Produce difference in series
gen deterrence = fit1 - out if dt >= `treatment_period'

** Time discount the difference at 10% 
local r = 1.1^(1/12)

gen deterrence_disc = deterrence*1/(`r'^(dt-`treatment_period'))
replace deterrence_disc = . if dt-`treatment_period' >= 60
count if !mi(deterrence_disc)
assert(r(N)==60)

*** Compute total
log using deterrence.log, replace
preserve
collapse (sum) deterrence_disc
qui summarize deterrence_disc
local deterrence = r(min)
disp `deterrence'
restore
log close

*** Truncate graph at 5 years 
drop if dt  - `treatment_period' >= 60
summarize dt 
local minperiod = r(min)
local maxperiod = r(max)

*** Settlement date: June 29, 2006 
local settlement = mofd(mdy(6,29,2006))

format dt %tmMon_YY
twoway (scatter pmt_amt dt, mcolor(gs5)) (line out fit1 dt, lwidth(thick thick) lcolor(navy maroon)), xline(`treatment_period') xline(`settlement', lcolor(black)) legend(order(2 "Outliers" 3 "Synthetic Control") width(80)) xscale(range(`minperiod' `maxperiod')) xlabel(468 492 `treatment_period' 540 564) xtitle("Month") ytitle("$/Month") graphregion(color(white)) yscale(range(0 1000000000)) ylabel(0 "$0" 300000000 "$300 Mil" 600000000 "$600 Mil" 900000000 "$900 Mil")

*graph save "graphs/OutliersOverlay.gph", replace
*graph export "graphs/OutliersOverlay.pdf", replace

*** Outliers only, no fit
twoway (scatter pmt_amt dt, mcolor(gs5)) (line out dt, lwidth(thick thick) lcolor(navy)), xline(`treatment_period') xline(`settlement', lcolor(black)) xscale(range(`minperiod' `maxperiod')) xlabel(468 492 `treatment_period' 540 564) xtitle("Month") ytitle("$/Month") graphregion(color(white)) yscale(range(0 1000000000)) ylabel(0 "$0" 300000000 "$300 Mil" 600000000 "$600 Mil" 900000000 "$900 Mil") legend(off)
graph export "graphs/OutliersOnly.pdf"
